package com.polaris.live.widget.marqueen

import android.content.Context
import android.view.LayoutInflater
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SpanUtils
import com.polaris.live.R
import com.polaris.live.popup.PriceModificationPromptPopup
import com.polaris.live.resp.back_resp.LiveRemindResp

/**
 * 简单字幕
 */
class SimpleNoticeTextView(context: Context) : MarqueeFactory<TextView?, LiveRemindResp>(context) {
    private val inflater: LayoutInflater

    init {
        inflater = LayoutInflater.from(context)
    }

    override fun generateMarqueeItemView(data: LiveRemindResp): TextView {
        val view = inflater.inflate(R.layout.layout_notice_marqueen_item_text, null) as TextView
        view.text = createSpan(view, data.content).create()
        return view
    }

    private fun createSpan(textView: TextView, message: String): SpanUtils {
        val span = SpanUtils.with(textView)
        val splitMessage = message.split("<#>")
        splitMessage.forEach { msg ->
            if (msg.contains(PriceModificationPromptPopup.TEXT)) {
                val modifiedStr = msg.replace(PriceModificationPromptPopup.TEXT, "")
                span.append(modifiedStr)
                    .setForegroundColor(
                        ContextCompat.getColor(
                            mContext,
                            R.color.color_ff8612
                        )
                    )
            } else {
                span.append(msg)
                    .setForegroundColor(
                        ContextCompat.getColor(
                            mContext,
                            R.color.color_000000_30
                        )
                    )
            }
        }
        return span
    }
}